package wlt.plan.view;

import javafx.scene.image.Image;
import javafx.scene.layout.*;

import java.io.File;

public class BackPhoto {
    public static void setBackgroundImage(Pane root) {
        // 获取项目根目录下的 photo/background 文件夹
        String folderPath = System.getProperty("user.dir") + "/photo/background";
        File bgDir = new File(folderPath);
        File[] files = bgDir.listFiles((dir, name) -> {
            String lower = name.toLowerCase();
            return lower.endsWith(".png") || lower.endsWith(".jpg") || lower.endsWith(".jpeg");
        });
        
        if (files != null && files.length > 0) {
            // 支持中文名
            Image bgImage = new Image(files[0].toURI().toString());
            BackgroundImage backgroundImage = new BackgroundImage(
                bgImage,
                BackgroundRepeat.NO_REPEAT,
                BackgroundRepeat.NO_REPEAT,
                BackgroundPosition.CENTER,
                new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, false, false, true, false)
            );
            root.setBackground(new Background(backgroundImage));
        } else {
            // 没有图片则不设置背景
            root.setBackground(null);
        }
    }
}
